package odfconverter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlWriter extends DefaultHandler {
	//protected DefaultHandler nextWriter;
	// private Map namespaceMapping = new HashMap();
	//private Vector<String> tagName;

	public void startDocument() throws SAXException {
		//tagName = new Vector<String>();
	}

	// 结束解析XML文件
	public void endDocument() throws SAXException {
		//for (int i = 0; i < tagName.size(); i++) {
		//	if (!tagName.get(i).equals("") || tagName.get(i) != null) {
		//		System.out.println("节点名称：" + tagName.get(i));
		//		System.out.println("节点值：" + tagValue.get(i));
		//	}
		//}
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// <pzip:archive pzip:target="C:\Documents and
		// Settings\Administrator\Local Settings\Temp\tmpDD4.tmp"
		// xmlns:pzip="urn:cleverage:xmlns:post-processings:zip"
		// xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
		// xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
		// xmlns:xlink="http://www.w3.org/1999/xlink"
		// xmlns:v="urn:schemas-microsoft-com:vml"
		// xmlns:o="urn:schemas-microsoft-com:office:office">
		
		// uri=urn:cleverage:xmlns:post-processings:zip;
		// localName=archive
		// qName=pzip:archive
		
		//在odfconverter中
		//WriteStartElement
		//prefix	"pzip"	
		//localName	"archive"	
		//ns	"urn:cleverage:xmlns:post-processings:zip"	
		
		//WriteStartAttribute
		//prefix	"pzip"	string
		//localName	"target"	string
		//ns	"urn:cleverage:xmlns:post-processings:zip"	string
		
		//WriteString
		//text	"C:\\Documents and Settings\\dqs\\Local Settings\\Temp\\tmp2A16.tmp"	string
		
		//WriteEndAttribute
        if(qName.indexOf(':')>-1){
        	WriteStartElement(prefixFromqName(localName,qName), localName, uri);
        }else{
        	WriteStartElement("", localName, uri);
        }

		String attributesName,pre,ln,ns;
		for (int i = 0; i < attributes.getLength(); i++) {
			attributesName = attributes.getQName(i);
			int index = attributesName.indexOf(':');
			if(index >-1){
				pre = attributesName.substring(0, index);
				ln = attributesName.substring(index+1,attributesName.length());
				if(pre.equals("xmlns")){
					ns = "http://www.w3.org/2000/xmlns/";
				}else{
					ns = attributes.getURI(i);
				}
			}else{
				pre = "";
				ln = attributesName;
				if(attributesName.equals("xmlns")){
					ns = "http://www.w3.org/2000/xmlns/";
				}else{
				    ns = attributes.getURI(i);
				}
			}
			WriteStartAttribute(pre, ln, ns);
			WriteString(attributes.getValue(attributes.getQName(i)));
			WriteEndAttribute();
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		WriteEndElement();
	}

	public void characters(char ch[], int start, int length)
			throws SAXException {
		WriteString(new String(ch, start, length));
	}

	public void WriteStartElement(String prefix, String localName, String ns) {
		// this.nextWriter.WriteStartElement(prefix, localName, ns);
		System.out.println("WriteStartElement:"+prefix+"-"+localName+"-"+ns);
	}

	public void WriteEndElement() {
		// this.nextWriter.WriteEndElement();
		System.out.println("WriteEndElement");
	}

	public void WriteStartAttribute(String prefix, String localName, String ns) {
		// this.nextWriter.WriteStartAttribute(prefix, localName, ns);
		System.out.println("WriteStartAttribute:"+prefix+"-"+localName+"-"+ns);
	}

	public void WriteEndAttribute() {
		// this.nextWriter.WriteEndAttribute();
		System.out.println("WriteEndAttribute");
	}

	public void WriteString(String text) {
		// this.nextWriter.WriteString(text);
		System.out.println("WriteString:"+text);
	}
	
	public String prefixFromqName(String localName, String qName){
		// localName=archive
		// qName=pzip:archive
		int index = qName.indexOf(':');
		if(index>-1) {
			return qName.substring(0,index);
		}else{
			return qName;
		}
	}

    
	public static void main(String[] args) throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException {
		String filename = "e:/work2/output.xml";
		// String filename = "d:/test.xml";
		SAXParserFactory spf = SAXParserFactory.newInstance();
		spf.setNamespaceAware(true);
		spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
		try {
			SAXParser saxParser = spf.newSAXParser();
			saxParser.parse(new File(filename), new XmlWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
